import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import os

# 打开游览器
driver = webdriver.Chrome()
driver.maximize_window()

# 请求本地网页
url = 'file:///' + os.path.dirname(os.path.abspath(__file__)) + '/html/注册实例.html'
driver.get(url)

# 找到账号输入框并输入
driver.find_element(By.ID,'user').send_keys('test123456')
# 找到密码输入框并输入
driver.find_element(By.ID,'password').send_keys('tt123456')
# 找到电话号码输入框并输入
driver.find_element(By.ID,'tel').send_keys('15624353230')
# 找到邮箱输入框并输入
driver.find_element(By.ID,'email').send_keys('15624353230@163.com')

# 找到下拉框选择需要的选项并点击
time.sleep(2)
s1 = driver.find_element(By.ID,'select')
s1.click()
select = Select(s1)  # 创建select对象使用
select.select_by_visible_text('上海')  # 通过文本选择选项

# 弹出弹窗并确定
driver.find_element(By.ID,'alert').click()
time.sleep(2)
alert = driver.switch_to.alert  # 切换到alert窗口
alert.accept()   # 弹窗确认

# 找到注册用户按钮并点击
driver.find_element(By.XPATH,'//*[@id="zc"]/fieldset/button').click()

# 关闭游览器
time.sleep(2)
driver.quit()